package com.hitfm.iplay.activity;

import java.io.File;
import java.io.FilenameFilter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.hitfm.iplay.ActivityManger;
import com.hitfm.iplay.MusicInfoController;
import com.hitfm.iplay.MusicPlayerApp;
import com.hitfm.iplay.MusicPlayerService;
import com.hitfm.iplay.R;
import com.hitfm.iplay.db.DatabaseHelper;
import com.hitfm.iplay.db.FavoriteDAO;
import com.hitfm.iplay.db.RecentplayDAO;
import com.hitfm.iplay.util.ConvertNum;
import com.hitfm.iplay.util.SDCardOperation;

@SuppressWarnings("deprecation")
public class MysongActivity extends Activity {
	class FolderAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (folderListFiles == null || folderListFiles.length == 0) {
				return 0;
			} else if (null != folderListFiles) {
				return folderListFiles.length;
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return folderListFiles[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.list_folderitem, null);
			}
			File file = folderListFiles[position];
			view.setBackgroundColor(position % 2 == 0 ? Color.WHITE : 0xFFF5F5F5);
			ImageView fileicon = (ImageView) view.findViewById(R.id.list_item_fileicon);
			TextView filename = (TextView) view.findViewById(R.id.list_item_filename);
			TextView lastmodifydate = (TextView) view.findViewById(R.id.list_item_lastmodifydate);
			lastmodifydate.setText(ConvertNum.formatDateString(MysongActivity.this, file.lastModified()));
			TextView url = (TextView) view.findViewById(R.id.list_item_url);
			String pathString = file.getAbsolutePath();
			url.setText(pathString);
			if (file.isDirectory()) {
				fileicon.setImageResource(R.drawable.folder);
				File[] tempFiles = new File(pathString).listFiles();
				filename.setText(file.getName() + "( " + tempFiles.length + " )");
			} else if (file.isFile() && file.getName().endsWith(".mp3")) {
				fileicon.setImageResource(R.drawable.file_icon_mp3);
				filename.setText(file.getName());
			}

			return view;
		}

	}

	class MusicListAdapter extends SimpleCursorAdapter {
		private boolean isLoacalLayout;

		public MusicListAdapter(Context context, int layout, Cursor c, String[] from, int[] to, boolean isLoacalLayout) {
			super(context, layout, c, from, to, FLAG_REGISTER_CONTENT_OBSERVER);
			if (null == c || c.getCount() < 1) {
				Toast.makeText(MysongActivity.this, R.string.emptylist, Toast.LENGTH_SHORT).show();
				return;
			}
			this.isLoacalLayout = isLoacalLayout;
		}

		public void bindView(View view, Context context, Cursor cursor) {
			super.bindView(view, context, cursor);
			view.setBackgroundColor((cursor.getPosition() % 2 == 0) ? Color.WHITE : 0xFFF5F5F5);
			TextView num = (TextView) view.findViewById(R.id.list_item_num);
			num.setText("" + (cursor.getPosition() + 1));
			TextView url = (TextView) view.findViewById(R.id.list_item_url);
			String urlString = cursor.getString(cursor.getColumnIndex((isLoacalLayout == true ? Media.DATA : KEY_URL)));
			url.setText(urlString);
			TextView songname = (TextView) view.findViewById(R.id.list_item_song_name);
			String songnameString = cursor
					.getString(cursor.getColumnIndex((isLoacalLayout == true ? Media.TITLE : KEY_SONGNAME)));
			if (null == songname || ("").equals(songname)) {
				File file = new File(urlString);
				if (file.exists()) {
					songname.setText(file.getName());
				}
			} else {
				songname.setText(songnameString);
			}
			TextView album = (TextView) view.findViewById(R.id.list_item_album);
			album.setText(cursor.getString(cursor.getColumnIndex((isLoacalLayout == true ? Media.ALBUM : KEY_ALBUM))));
			TextView singer = (TextView) view.findViewById(R.id.list_item_singer);
			singer.setText(cursor.getString(cursor.getColumnIndex((isLoacalLayout == true ? Media.ARTIST : KEY_SINGER))));

		}
	}

	class SingerRootAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mSingerRootCursor.getCount();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			mSingerRootCursor.moveToPosition(position);
			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.list_singerrootitem, null);
			}
			view.setBackgroundColor(position % 2 == 0 ? Color.WHITE : 0xFFF5F5F5);
			ImageView fileicon = (ImageView) view.findViewById(R.id.list_singerrootitem_fileicon);
			TextView singername = (TextView) view.findViewById(R.id.list_singerrootitem_singername);
			TextView filecount = (TextView) view.findViewById(R.id.list_singerrootitem_filecount);
			Bitmap icon = mMusicPlayerService.getAlbumArt(
					mSingerRootCursor.getInt(mSingerRootCursor.getColumnIndex(Media.ALBUM_ID)), RADIUS30);
			fileicon.setImageBitmap(icon);
			singername.setText(mSingerRootCursor.getString(mSingerRootCursor.getColumnIndex(Media.ARTIST)));
			filecount.setText("( " + mSingerRootCursor.getInt(mSingerRootCursor.getColumnIndexOrThrow("count(*)")) + " )");
			return view;
		}
	}

	private static Thread adapterThread;
	private static final int ALLSONG = 0;
	private static final int DEL = 1;
	private static final int DEL_LIST = 1;
	private static final int DEL_SD = 2;
	private static final int FAVORITE_LAYOUT = 4;
	private static final int FOLDER = 1;
	private static File[] folderListFiles;
	private static File sdCardPath = new File(Environment.getExternalStorageDirectory().toString());
	private static String folderPath = sdCardPath.getAbsolutePath();
	private static Handler handler;
	private static final int INFO = 2;
	protected static Intent intent;
	private static boolean isLocalsongSingerDetailClicked = false;
	public static final String KEY_ALBUM = "album";
	public static final String KEY_ID = "_id";
	public static final String KEY_SINGER = "singer";
	public static final String KEY_SONGNAME = "songname";
	public static final String KEY_URL = "url";
	private static RelativeLayout layout_local_all_playing_id;
	private static LinearLayout layout_local_alllist;

	private static RelativeLayout layout_local_folder;
	private static LinearLayout layout_local_folderlist;
	private static LinearLayout layout_local_singerlist;
	private static RelativeLayout layout_localSongAll;
	private static LinearLayout layout_mySong;
	private static LinearLayout layout_recentplay;
	private static final int LIST_LAYOUT = 3;
	private static final String LISTMENU_DEL = "删除文件";
	private static final String LISTMENU_DEL_LIST = "列表删除";
	private static final String LISTMENU_DEL_SD = "磁盘删除";
	private static final String LISTMENU_INFO = "歌曲信息";
	private static final String LISTMENU_MODIFY = "修改信息";
	private static final String LISTMENU_PLAYALLSONGINFOLDER = "播放播放所有歌曲";

	private static final String LISTMENU_PLAY = "播放歌曲";
	protected static final int LOADING_DIALOG = 0;

	private static TextView local_all_playing_id;
	private static TextView local_allsong;
	private static ImageView local_backto_localsong;
	private static TextView local_folder;
	private static ImageView local_folder_pre;
	private static Button local_folder_sdcard;
	private static boolean local_ismoved = false;
	private static ListView local_list_all;
	private static ListView local_list_folder;
	private static ListView local_list_singer;
	private static float local_movedX = 0;
	private static float local_movedY = 0;
	private static int local_selected_button = 0;
	private static TextView local_singer;
	private static final int LOCALSONG_LAYOUT = 1;
	protected static FolderAdapter mFolderAdapter;
	private static Cursor mLocalCursor = null;
	private static MusicListAdapter mLocalMusicListAdapter;
	private static MusicInfoController mMusicInfoController = null;
	private static MusicPlayerService mMusicPlayerService = null;
	private static final int MODIFY = 3;
	private static final int MOVEDSC = 20;
	private static FilenameFilter mp3Filter;
	private static Cursor mRecentCursor;
	private static Cursor mFavoriteCursor;
	private static MusicListAdapter mRecentMusicListAdapter;
	protected static Cursor mSingerDetailCursor;
	protected static Cursor mSingerRootCursor;
	private static MusicPlayerApp musicPlayerApp;
	private static TextView mysong_favorite;
	private static final int MYSONG_LAYOUT = 0;
	private static TextView mysong_localsong;
	private static TextView mysong_playlist;
	private static TextView mysong_recentplay;
	private static final int PLAY = 0;;
	private static final int RADIUS30 = 40;
	private static ImageView recent_backto_localsong;
	private static TextView recent_clear;
	private static final int RECENT_LAYOUT = 2;
	private static ListView recent_list;
	private static ListView favorite_list;
	private static RecentplayDAO recentplayDAO;
	private static FavoriteDAO favoriteDAO;
	private static final int SINGER = 2;
	private static final int SINGER_DETAIL = 4;
	private static final int RECENT = 5;
	private static final int SINGER_ROOT = 3;
	private static final String TAG = "iplay";
	private static int unselected = -1;
	private static int updateList = unselected;
	private ProgressDialog dialog;
	private static int selTitle = 0;

	protected int layoutIndex = MYSONG_LAYOUT;

	private ServiceConnection mPlaybackConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			// mMusicPlayerService = ((MusicPlayerService.LocalBinder) service)
			// .getService();
			if (null != mMusicPlayerService && mMusicPlayerService.isPlaying()) {
				local_all_playing_id.setText(mMusicPlayerService.getInfo());
			}
		}

		public void onServiceDisconnected(ComponentName className) {
			mMusicPlayerService = null;
		}
	};

	protected BroadcastReceiver mPlayerEvtReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (null == mMusicPlayerService) {
				return;
			}
			String action = intent.getAction();
			Log.i(TAG, action);
			if (action.equals(MusicPlayerService.PLAYER_PREPARE_END) || action.equals(MusicPlayerService.UPDATE)) {
				// will begin to play
				local_all_playing_id.setText(mMusicPlayerService.isRandom() == true ? R.string.random_play : R.string.playing);
				local_all_playing_id.append("  " + mMusicPlayerService.getInfo());
				if (layoutIndex == RECENT_LAYOUT) {
					showRecentList();
				}
			} else if (action.equals(MusicPlayerService.DELETE)) {
				local_all_playing_id.setText(R.string.stopplaying);
			} else {
				local_all_playing_id.setText(R.string.stopplaying);
			}
		}
	};
	private static ImageView favorite_backto_localsong;
	private static TextView favorite_clear;
	private static LinearLayout layout_favorite;

	protected static MusicListAdapter mSingerDetailAdapter;
	protected static SingerRootAdapter mSingerRootAdapter;
	protected static MusicListAdapter mFavoriteMusicListAdapter;
	private static String singerArtist = "";

	protected void backToMysong() {
		layout_mySong.setVisibility(View.VISIBLE);
		layout_localSongAll.setVisibility(View.INVISIBLE);
		layout_recentplay.setVisibility(View.INVISIBLE);
		layout_favorite.setVisibility(View.INVISIBLE);
		layoutIndex = MYSONG_LAYOUT;
	}

	private void deleteSong(final DatabaseHelper db, final String url) {
		AlertDialog.Builder builder = new AlertDialog.Builder(MysongActivity.this).setMessage(R.string.deleteconfirm)
				.setTitle(R.string.tip).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						try {
							deleteSongOnlyDB(db, url);
							showRecentList();
							File file = new File(url);
							if (file.exists()) {
								if (mMusicPlayerService.getMusicPath() != null && mMusicPlayerService.getMusicPath().equals(url)) {
									mMusicPlayerService.stop();
								}
								Toast.makeText(MysongActivity.this,
										file.delete() == true ? R.string.deletefileok : R.string.deletefileerror,
										Toast.LENGTH_SHORT).show();
								if (mMusicInfoController.deleteByURL(url) > 0) {
									mMusicPlayerService.setDeleteSong(true);
									updateList = ALLSONG;
									showAllSongList();
									local_list_all.setAdapter(mLocalMusicListAdapter);
								}
							} else {
								Toast.makeText(MysongActivity.this, R.string.notexists, Toast.LENGTH_SHORT).show();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
						dialog.dismiss();
					}
				}).setNegativeButton(R.string.cancle, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();

	}

	private void deleteSongOnlyDB(DatabaseHelper db, String url) {
		db.open();
		if (db.deleteDataByURL(url)) {
			Toast.makeText(this, R.string.deletedbok, Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, R.string.deletedberror, Toast.LENGTH_SHORT).show();
		}
		if (db != null) {
			db.close();
		}
	}

	private void findMySongViews() {
		// 我的歌曲
		layout_mySong = (LinearLayout) findViewById(R.id.layout_mysong);
		mysong_localsong = (TextView) findViewById(R.id.local_song);
		mysong_recentplay = (TextView) findViewById(R.id.recent_play);
		mysong_playlist = (TextView) findViewById(R.id.playlist);
		mysong_favorite = (TextView) findViewById(R.id.favorite);
		// 本地歌曲
		layout_local_all_playing_id = (RelativeLayout) findViewById(R.id.layout_localsong_all_playing_id);
		layout_local_folder = (RelativeLayout) findViewById(R.id.layout_localsong_folder);
		layout_localSongAll = (RelativeLayout) findViewById(R.id.layout_localsong_all);
		layout_local_alllist = (LinearLayout) findViewById(R.id.layout_localsong_alllist);
		layout_local_folderlist = (LinearLayout) findViewById(R.id.layout_localsong_folderlist);
		layout_local_singerlist = (LinearLayout) findViewById(R.id.layout_localsong_singerlist);
		local_backto_localsong = (ImageView) findViewById(R.id.local_backto_mysong);
		local_allsong = (TextView) findViewById(R.id.allsong);
		local_folder = (TextView) findViewById(R.id.folder);
		local_singer = (TextView) findViewById(R.id.singer);
		local_list_singer = (ListView) findViewById(R.id.list_localsong_singer);
		local_all_playing_id = (TextView) findViewById(R.id.playing_id);
		local_list_all = (ListView) findViewById(R.id.list_localsong_all);
		local_list_folder = (ListView) findViewById(R.id.list_localsong_folder);
		local_folder_sdcard = (Button) findViewById(R.id.localsong_folder_sdcard);
		local_folder_pre = (ImageView) findViewById(R.id.localsong_folder_pre);
		// 最近播放
		layout_recentplay = (LinearLayout) findViewById(R.id.layout_recentplay);
		recent_backto_localsong = (ImageView) findViewById(R.id.recent_backto_mysong);
		recent_clear = (TextView) findViewById(R.id.recentplay_clear);
		recent_list = (ListView) findViewById(R.id.list_recent);
		// 我的收藏
		layout_favorite = (LinearLayout) findViewById(R.id.layout_favorite);
		favorite_backto_localsong = (ImageView) findViewById(R.id.favorite_backto_mysong);
		favorite_clear = (TextView) findViewById(R.id.favorite_clear);
		favorite_list = (ListView) findViewById(R.id.list_favorite);
	}

	protected void initService() {
		musicPlayerApp = (MusicPlayerApp) getApplication();
		mMusicInfoController = musicPlayerApp.getMusicInfoController();

		startService(new Intent(this, MusicPlayerService.class));
		getApplicationContext().bindService(new Intent(this, MusicPlayerService.class), mPlaybackConnection,
				Context.BIND_AUTO_CREATE);

		IntentFilter filter = new IntentFilter();
		filter.addAction(MusicPlayerService.PLAYER_PREPARE_END);
		filter.addAction(MusicPlayerService.PLAY_COMPLETED);
		filter.addAction(MusicPlayerService.PLAY_PAUSE);
		filter.addAction(MusicPlayerService.PLAY_CONTINUE);
		filter.addAction(MusicPlayerService.ERROR);
		registerReceiver(mPlayerEvtReceiver, filter);

	}

	private void modifySongInfo(final String url) {
		Cursor cursor = mMusicInfoController.getSingleSongByURL(url);
		if (cursor != null && cursor.getCount() == 1) {
			cursor.moveToFirst();
			String filename = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));
			String songname = cursor.getString(cursor.getColumnIndex(Media.TITLE));
			String singer = cursor.getString(cursor.getColumnIndex(Media.ARTIST));
			String album = cursor.getString(cursor.getColumnIndex(Media.ALBUM));
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.modifysonginfo, (ViewGroup) findViewById(R.id.modifysonginfo));
			final EditText filenameEditText = (EditText) layout.findViewById(R.id.modifyinfo_filename);
			final EditText songnameEditText = (EditText) layout.findViewById(R.id.modifyinfo_songname);
			final EditText singerEditText = (EditText) layout.findViewById(R.id.modifyinfo_singer);
			final EditText albumEditText = (EditText) layout.findViewById(R.id.modifyinfo_album);
			filenameEditText.setText(filename.replace(".mp3", ""));
			songnameEditText.setText(songname);
			singerEditText.setText(singer);
			albumEditText.setText(album);
			new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							if (url.equals(mMusicPlayerService.getMusicPath())) {
								Toast.makeText(MysongActivity.this, R.string.thesongisplaying, Toast.LENGTH_SHORT).show();
								return;
							}
							String fnameString = filenameEditText.getText().toString().trim() + ".mp3";
							String snameString = songnameEditText.getText().toString().trim();
							String singerString = singerEditText.getText().toString().trim();
							String albumString = albumEditText.getText().toString().trim();
							int updateRows = 0;
							if (fnameString.length() > 4) {
								try {
									MusicPlayerService.setSinger(singerString);
									MusicPlayerService.setSongname(snameString);
									MusicPlayerService.setAlbum(albumString);
									updateRows = mMusicInfoController.updateSongInfo(url, fnameString, snameString, singerString,
											albumString);
								} catch (Exception e) {
									e.printStackTrace();
								}
								updateList = ALLSONG;
								showAllSongList();
								local_list_all.setAdapter(mLocalMusicListAdapter);
							} else {
								Toast.makeText(MysongActivity.this, R.string.filenameisempty, Toast.LENGTH_SHORT).show();
							}
							if (updateRows != 1) {
								Toast.makeText(MysongActivity.this, R.string.updatesonginfoerror, Toast.LENGTH_SHORT).show();
							} else if (updateRows == 1) {
								Toast.makeText(MysongActivity.this, R.string.updatesonginfook, Toast.LENGTH_SHORT).show();
								mMusicPlayerService.setUpdateSongInfo(true);
							}
						}
					}).setNegativeButton("取消", null).show();
		} else {
			Toast.makeText(this, R.string.noinfo, Toast.LENGTH_SHORT).show();
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mysong);
		findMySongViews();
		setMySongListeners();
		setBroadcast();
		ActivityManger.getInstance().addActivity(this);
		mMusicPlayerService = MusicPlayerApp.getmMusicPlayerService();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case ALLSONG:
					local_list_all.setAdapter(mLocalMusicListAdapter);
					local_list_all.setDivider(null);
					setLocalsongAllListListner();
					break;

				case FOLDER:
					local_list_folder.setAdapter(mFolderAdapter);
					local_list_all.setDivider(null);
					setLocalsongFolderListListner();
					break;
				case SINGER_ROOT:
					local_list_singer.setAdapter(mSingerRootAdapter);
					local_list_singer.setDivider(null);
					setLocalsongSingerRootListListner();
					break;
				case SINGER_DETAIL:
					local_list_singer.setAdapter(mSingerDetailAdapter);
					local_list_singer.setDivider(null);
					setLocalsongSingerDetailListListner();
					break;
				default:
					break;
				}
				removeDialog(LOADING_DIALOG);
			}
		};
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {
		case LOADING_DIALOG:
			dialog = new ProgressDialog(this);
			dialog.setMessage("加载中...");
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			return dialog;
		default:
			break;
		}
		return dialog;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// musicPlayerApp.closeCursor();
		// unbindService(mPlaybackConnection);
		stopService(new Intent(this, MusicPlayerService.class));
		// unregisterReceiver(mPlayerEvtReceiver);
		if (null != mRecentCursor) {
			mRecentCursor.close();
			mRecentCursor = null;
		}
		if (null != recentplayDAO) {
			recentplayDAO.close();
			recentplayDAO = null;
		}
		if (null != favoriteDAO) {
			favoriteDAO.close();
			favoriteDAO = null;
		}
	}

	private void onItemClickListnerPlay(View clicked, int position) {
		String url = ((TextView) clicked.findViewById(R.id.list_item_url)).getText().toString();
		File temp = new File(url);
		TextView song_name = (TextView) clicked.findViewById(R.id.list_item_song_name);
		TextView singer = (TextView) clicked.findViewById(R.id.list_item_singer);
		TextView album = (TextView) clicked.findViewById(R.id.list_item_album);
		MusicPlayerService.setId(position);
		if (song_name == null) {
			MediaMetadataRetriever retriever = new MediaMetadataRetriever();
			retriever.setDataSource(url);
			retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
			MusicPlayerService.setSinger(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
			MusicPlayerService.setSongname(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
			MusicPlayerService.setAlbum(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
		} else {
			MusicPlayerService.setSinger(singer.getText().toString());
			MusicPlayerService.setSongname(song_name.getText().toString());
			MusicPlayerService.setAlbum(album.getText().toString());
		}
		if (mMusicPlayerService != null && mMusicPlayerService.getMusicPath() == url && mMusicPlayerService.isPlaying()) {
			mMusicPlayerService.pause();
		} else if (mMusicPlayerService != null && temp.exists()) {
			mMusicPlayerService.stop();
			mMusicPlayerService.setMusicPath(url);
			mMusicPlayerService.start();
		} else if (!temp.exists()) {
			Toast.makeText(MysongActivity.this, R.string.notexists, Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(MysongActivity.this, R.string.unknowerror, Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && layoutIndex != MYSONG_LAYOUT) {
			backToMysong();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	protected void onRestart() {

		super.onRestart();
		if (null != mLocalMusicListAdapter) {
			musicPlayerApp.initCursor();
			mLocalCursor = musicPlayerApp.getCursor();
			mLocalMusicListAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void onResume() {
		switch (layoutIndex) {
		case MYSONG_LAYOUT:

			break;
		case RECENT_LAYOUT:
			showRecentList();
			break;
		case FAVORITE_LAYOUT:
			showFavoriteList();
		default:
			break;
		}
		super.onResume();
	}

	private void playSong(String url, final View clicked, final int position) {
		if (mMusicPlayerService.getMusicPath() != null && mMusicPlayerService.getMusicPath().equals(url)) {
			Toast.makeText(MysongActivity.this, R.string.musicisplaying, Toast.LENGTH_SHORT).show();
		} else {
			onItemClickListnerPlay(clicked, position);
		}
	}

	protected void recent_showRecentlist() {

	}

	private void setBroadcast() {
		IntentFilter playerFilter = new IntentFilter();
		playerFilter.addAction(MusicPlayerService.PLAYER_PREPARE_END);
		playerFilter.addAction(MusicPlayerService.UPDATE);
		playerFilter.addAction(MusicPlayerService.DELETE);
		registerReceiver(mPlayerEvtReceiver, playerFilter);

	}

	private void setLocalsongAllListListner() {
		local_list_all.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				onItemClickListnerPlay(clicked, position);
			}

		});

		local_list_all.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, final View clicked, final int position, long arg3) {
				final String url = ((TextView) clicked.findViewById(R.id.list_item_url)).getText().toString();
				new AlertDialog.Builder(MysongActivity.this)
						.setTitle(R.string.listmenu)
						.setItems(new String[] { LISTMENU_PLAY, LISTMENU_DEL, LISTMENU_INFO, LISTMENU_MODIFY },
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {
										switch (which) {
										case PLAY:
											playSong(url, clicked, position);
											break;
										case DEL:
											deleteSong(recentplayDAO, url);
											break;
										case INFO:
											showSongInfo(url);
											break;
										case MODIFY:
											modifySongInfo(url);
											break;
										default:
											break;
										}
									}
								}).show();
				return false;

			}
		});
	}

	private void setLocalsongFolderListListner() {
		local_list_folder.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				final String url = ((TextView) clicked.findViewById(R.id.list_item_url)).getText().toString();
				File file = new File(url);
				if (!file.exists()) {
					Toast.makeText(MysongActivity.this, R.string.nosong, Toast.LENGTH_SHORT).show();
				}
				if (file.isDirectory()) {
					showDialog(LOADING_DIALOG);
					adapterThread = new Thread() {
						public void run() {
							Looper.prepare();
							if (local_list_folder != null) {
								showFolder(url);
								handler.sendEmptyMessage(FOLDER);
							}
							Looper.loop();
						}
					};
					adapterThread.start();
				} else if (file.isFile() && url.endsWith(".mp3")) {
					mMusicPlayerService.setPlayingNotFromLocalList(true);
					playSong(url, clicked, position);
				}
			}
		});
		local_list_folder.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				final String url = ((TextView) clicked.findViewById(R.id.list_item_url)).getText().toString();
				new AlertDialog.Builder(MysongActivity.this).setTitle(R.string.listmenu)
						.setItems(new String[] { LISTMENU_PLAYALLSONGINFOLDER }, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								String urlString = "";
								if (new File(url).isFile()) {
									url.substring(0, url.lastIndexOf("/"));
								} else {
									urlString = url;
								}
								Cursor cursor = mMusicInfoController.getAllSongFolder(urlString);
								if (cursor == null || cursor.getCount() == 0) {
									Toast.makeText(MysongActivity.this, R.string.noexistsong, Toast.LENGTH_SHORT).show();
								} else {
									musicPlayerApp.setCursor(cursor);
								}
								mMusicPlayerService.stop();
								mMusicPlayerService.nextOrPreSong(true);
							}

						}).show();
				return false;
			}

		});
	}

	protected void setLocalsongSingerDetailListListner() {
		local_list_singer.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				TextView urlTextView = (TextView) clicked.findViewById(R.id.list_item_url);
				playSong(urlTextView.getText().toString(), clicked, position);
			}
		});

	}

	protected void setLocalsongSingerRootListListner() {
		local_list_singer.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				isLocalsongSingerDetailClicked = true;
				final TextView artistTextView = (TextView) clicked.findViewById(R.id.list_singerrootitem_singername);
				showDialog(LOADING_DIALOG);
				adapterThread = new Thread() {
					public void run() {
						Looper.prepare();
						if (local_list_singer != null) {
							showSingerDetailList(artistTextView.getText().toString());
							handler.sendEmptyMessage(SINGER_DETAIL);
						}
						Looper.loop();
					}
				};
				adapterThread.start();
			}
		});
		local_list_singer.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				return false;
			}
		});

	}

	private void setMySongListeners() {
		// 我的歌曲
		mysong_localsong.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				initService();
				layoutIndex = LOCALSONG_LAYOUT;
				layout_mySong.setVisibility(View.INVISIBLE);
				layout_localSongAll.setVisibility(View.VISIBLE);
				layout_recentplay.setVisibility(View.INVISIBLE);
				if (mLocalMusicListAdapter == null) {
					showDialog(LOADING_DIALOG);
					adapterThread = new Thread() {
						public void run() {
							Looper.prepare();
							if (local_list_all != null) {
								showAllSongList();
								handler.sendEmptyMessage(ALLSONG);
							}
							Looper.loop();
						}
					};
					adapterThread.start();
				}
			}
		});
		mysong_recentplay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				recentplayDAO = new RecentplayDAO(MysongActivity.this);
				showRecentList();
				layoutIndex = RECENT_LAYOUT;
				layout_mySong.setVisibility(View.INVISIBLE);
				layout_localSongAll.setVisibility(View.INVISIBLE);
				layout_recentplay.setVisibility(View.VISIBLE);
			}
		});
		mysong_favorite.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				favoriteDAO = new FavoriteDAO(MysongActivity.this);
				showFavoriteList();
				layoutIndex = FAVORITE_LAYOUT;
				layout_mySong.setVisibility(View.INVISIBLE);
				layout_localSongAll.setVisibility(View.INVISIBLE);
				layout_recentplay.setVisibility(View.INVISIBLE);
				layout_favorite.setVisibility(View.VISIBLE);
			}
		});
		// 本地歌曲
		local_backto_localsong.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!isLocalsongSingerDetailClicked) {
					backToMysong();
				} else {
					showDialog(LOADING_DIALOG);
					handler.sendEmptyMessage(SINGER_ROOT);
					isLocalsongSingerDetailClicked = false;
				}
			}
		});
		local_allsong.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (local_selected_button == ALLSONG) {
					return false;
				}
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					local_movedX = event.getX();
					local_movedY = event.getY();
					local_allsong.setBackgroundResource(R.drawable.all_selected);
					break;
				case MotionEvent.ACTION_UP:
					if (MysongActivity.local_ismoved) {
						local_allsong.setBackgroundResource(R.drawable.all_unselected);
					} else {
						layout_local_all_playing_id.setVisibility(View.VISIBLE);
						layout_local_alllist.setVisibility(View.VISIBLE);
						layout_local_folderlist.setVisibility(View.INVISIBLE);
						layout_local_folder.setVisibility(View.INVISIBLE);
						layout_local_singerlist.setVisibility(View.INVISIBLE);
						local_folder.setBackgroundResource(R.drawable.folder_unselected);
						local_singer.setBackgroundResource(R.drawable.singer_unselected);
						local_selected_button = ALLSONG;
					}
					MysongActivity.local_ismoved = false;
					break;
				case MotionEvent.ACTION_MOVE:
					if (Math.abs((event.getX() - local_movedX)) > MOVEDSC || Math.abs((event.getY() - local_movedY)) > MOVEDSC) {
						MysongActivity.local_ismoved = true;
					}
					break;
				default:
					break;
				}
				return true;
			}
		});
		local_folder.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (local_selected_button == FOLDER) {
					return false;
				}
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					local_movedX = event.getX();
					local_movedY = event.getY();
					local_folder.setBackgroundResource(R.drawable.folder_selected);
					break;
				case MotionEvent.ACTION_UP:
					if (MysongActivity.local_ismoved) {
						local_folder.setBackgroundResource(R.drawable.folder_unselected);
					} else {
						if (mFolderAdapter == null) {
							showDialog(LOADING_DIALOG);
							adapterThread = new Thread() {
								public void run() {
									Looper.prepare();
									if (local_list_folder != null) {
										showFolder(sdCardPath.toString());
										handler.sendEmptyMessage(FOLDER);
									}
									Looper.loop();
								}
							};
							adapterThread.start();
						}
						layout_local_all_playing_id.setVisibility(View.INVISIBLE);
						layout_local_folder.setVisibility(View.VISIBLE);
						layout_local_alllist.setVisibility(View.INVISIBLE);
						layout_local_folderlist.setVisibility(View.VISIBLE);
						layout_local_singerlist.setVisibility(View.INVISIBLE);
						local_allsong.setBackgroundResource(R.drawable.all_unselected);
						local_singer.setBackgroundResource(R.drawable.singer_unselected);
						local_selected_button = FOLDER;
					}
					MysongActivity.local_ismoved = false;
					break;
				case MotionEvent.ACTION_MOVE:
					if (Math.abs((event.getX() - local_movedX)) > MOVEDSC || Math.abs((event.getY() - local_movedY)) > MOVEDSC) {
						MysongActivity.local_ismoved = true;
					}
					break;
				default:
					break;
				}
				return true;
			}
		});
		local_singer.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if (local_selected_button == SINGER) {
					return false;
				}
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					local_movedX = event.getX();
					local_movedY = event.getY();
					local_singer.setBackgroundResource(R.drawable.singer_selected);
					break;
				case MotionEvent.ACTION_UP:
					if (MysongActivity.local_ismoved) {
						local_singer.setBackgroundResource(R.drawable.singer_unselected);
					} else {
						layout_local_all_playing_id.setVisibility(View.INVISIBLE);
						layout_local_folder.setVisibility(View.INVISIBLE);
						layout_local_alllist.setVisibility(View.INVISIBLE);
						layout_local_folderlist.setVisibility(View.INVISIBLE);
						layout_local_singerlist.setVisibility(View.VISIBLE);
						local_allsong.setBackgroundResource(R.drawable.all_unselected);
						local_folder.setBackgroundResource(R.drawable.folder_unselected);
						local_selected_button = SINGER;
						if (mSingerRootAdapter == null) {
							showDialog(LOADING_DIALOG);
							adapterThread = new Thread() {
								public void run() {
									Looper.prepare();
									if (local_list_singer != null) {
										showSingerRootList();
										handler.sendEmptyMessage(SINGER_ROOT);
									}
									Looper.loop();
								}
							};
							adapterThread.start();
						}
					}
					MysongActivity.local_ismoved = false;
					break;
				case MotionEvent.ACTION_MOVE:
					if (Math.abs((event.getX() - local_movedX)) > MOVEDSC || Math.abs((event.getY() - local_movedY)) > MOVEDSC) {
						MysongActivity.local_ismoved = true;
					}
					break;
				default:
					break;
				}
				return true;
			}
		});
		local_folder_sdcard.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!folderPath.equals(sdCardPath.getAbsolutePath())) {
					showFolder(sdCardPath.toString());
					local_list_folder.setAdapter(mFolderAdapter);
				}
			}
		});
		local_folder_pre.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!folderPath.equals(sdCardPath.getAbsolutePath())
						&& folderPath.length() >= sdCardPath.getAbsolutePath().length()) {
					showDialog(LOADING_DIALOG);
					showFolder(folderPath.substring(0, folderPath.lastIndexOf("/")));
					handler.sendEmptyMessage(FOLDER);
				} else if (folderPath.equals(sdCardPath.getAbsolutePath())) {
					Toast.makeText(MysongActivity.this, R.string.rootdirectory, Toast.LENGTH_SHORT).show();
				}
			}
		});
		// 最近播放
		recent_backto_localsong.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				backToMysong();
			}
		});
		recent_clear.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				RecentplayDAO recentplayDAO = new RecentplayDAO(getBaseContext());
				recentplayDAO.open();
				recentplayDAO.dropTable();
				recentplayDAO.close();
				Toast.makeText(MysongActivity.this, R.string.listemptyed, Toast.LENGTH_SHORT).show();
				// 刷新列表
				showRecentList();
			}
		});
		// 我的收藏
		favorite_backto_localsong.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				backToMysong();
			}
		});
		favorite_clear.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				favoriteDAO.open();
				favoriteDAO.dropTable();
				favoriteDAO.close();
				Toast.makeText(MysongActivity.this, R.string.listemptyed, Toast.LENGTH_SHORT).show();
				showFavoriteList();
			}
		});
	}

	private void setRecentListListner() {
		recent_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				mMusicPlayerService.setPlayingNotFromLocalList(true);
				TextView urlView = (TextView) clicked.findViewById(R.id.list_item_url);
				String url = urlView.getText().toString();
				playSong(url, clicked, position);
			}
		});
		recent_list.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, final View clicked, final int position, long arg3) {
				final String url = ((TextView) clicked.findViewById(R.id.list_item_url)).getText().toString();
				new AlertDialog.Builder(MysongActivity.this)
						.setTitle(R.string.listmenu)
						.setItems(new String[] { LISTMENU_PLAY, LISTMENU_DEL_LIST, LISTMENU_DEL_SD },
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {
										switch (which) {
										case PLAY:
											playSong(url, clicked, position);
											break;
										case DEL_LIST:
											deleteSongOnlyDB(recentplayDAO, url);
											showRecentList();
											break;
										case DEL_SD:
											deleteSong(recentplayDAO, url);
											showRecentList();
											break;
										default:
											break;
										}
									}

								}).show();
				return false;
			}
		});
	}

	protected void showAllSongList() {
		if (null == musicPlayerApp.getCursor() || updateList == ALLSONG) {
			musicPlayerApp.initCursor();
		}
		mLocalCursor = musicPlayerApp.getCursor();
		if (null == mLocalCursor || mLocalCursor.isClosed()) {
			Toast.makeText(this, R.string.unknowerror, Toast.LENGTH_SHORT).show();
			return;
		}
		if (mLocalCursor.getCount() == 0) {
			local_all_playing_id.setText(R.string.nosong);
			return;
		}
		mLocalMusicListAdapter = new MusicListAdapter(this, R.layout.list_musicitem, mLocalCursor, new String[] {}, new int[] {},
				true);
	}

	protected void showFolder(final String path) {
		if (!SDCardOperation.IsExistSdCard()) {
			Toast.makeText(this, R.string.nosdcard, Toast.LENGTH_SHORT).show();
			return;
		}
		if (folderPath.equals(path) && mFolderAdapter != null) {
			local_list_folder.setAdapter(mFolderAdapter);
			return;
		} else if (mFolderAdapter == null) {
			mFolderAdapter = new FolderAdapter();
		}
		folderPath = path;
		mp3Filter = new FilenameFilter() {
			File tempFile;

			@Override
			public boolean accept(File dir, String filename) {
				tempFile = new File(dir.getPath() + "/" + filename);
				if (tempFile.isDirectory() && !filename.startsWith(".") && !tempFile.isHidden()
						&& tempFile.listFiles().length > 0) {
					return true;
				} else if (tempFile.isFile() && filename.endsWith(".mp3")) {
					return true;
				}
				return false;
			}
		};
		File file = new File(path);
		folderListFiles = file.listFiles(mp3Filter);
		if (mFolderAdapter.getCount() == 0) {
			Toast.makeText(MysongActivity.this, R.string.noexistsong, Toast.LENGTH_SHORT).show();
		}
	}

	protected void showRecentList() {
		recentplayDAO.open();
		mRecentCursor = recentplayDAO.getAllDataByDESC();
		if (null == mMusicPlayerService) {
			return;
		}
		mRecentMusicListAdapter = new MusicListAdapter(this, R.layout.list_musicitem, mRecentCursor, new String[] {},
				new int[] {}, false);
		if (null != recentplayDAO) {
			recentplayDAO.close();
		}
		recent_list.setAdapter(mRecentMusicListAdapter);
		recent_list.setDivider(null);
		setRecentListListner();
	}

	private void showSingerDetailList(String artist) {
		singerArtist = artist;
		mSingerDetailCursor = mMusicInfoController.getAllSongsByArtist(artist);
		if (mSingerDetailCursor == null || mSingerDetailCursor.getCount() < 1) {
			Toast.makeText(this, R.string.nosong, Toast.LENGTH_SHORT).show();
			return;
		}
		mSingerDetailCursor.moveToFirst();
		mSingerDetailAdapter = new MusicListAdapter(this, R.layout.list_musicitem, mSingerDetailCursor, new String[] {},
				new int[] {}, true);
	}

	protected void showFavoriteList() {
		favoriteDAO.open();
		mFavoriteCursor = favoriteDAO.getAllDataByDESC();
		if (null == mMusicPlayerService) {
			return;
		}
		mFavoriteMusicListAdapter = new MusicListAdapter(this, R.layout.list_musicitem, mFavoriteCursor, new String[] {},
				new int[] {}, false);
		if (null != favoriteDAO) {
			favoriteDAO.close();
		}
		favorite_list.setAdapter(mFavoriteMusicListAdapter);
		favorite_list.setDivider(null);
		setFavoriteListListner();
	}

	private void setFavoriteListListner() {
		favorite_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View clicked, int position, long arg3) {
				mMusicPlayerService.setPlayingNotFromLocalList(true);
				TextView urlView = (TextView) clicked.findViewById(R.id.list_item_url);
				String url = urlView.getText().toString();
				playSong(url, clicked, position);
			}
		});
		favorite_list.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, final View clicked, final int position, long arg3) {
				final String url = ((TextView) clicked.findViewById(R.id.list_item_url)).getText().toString();
				new AlertDialog.Builder(MysongActivity.this)
						.setTitle(R.string.listmenu)
						.setItems(new String[] { LISTMENU_PLAY, LISTMENU_DEL_LIST, LISTMENU_DEL_SD },
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog, int which) {
										switch (which) {
										case PLAY:
											playSong(url, clicked, position);
											break;
										case DEL_LIST:
											deleteSongOnlyDB(favoriteDAO, url);
											showFavoriteList();
											break;
										case DEL_SD:
											deleteSong(favoriteDAO, url);
											showFavoriteList();
											break;
										default:
											break;
										}
									}

								}).show();
				return false;
			}
		});

	}

	private void showSingerRootList() {
		mSingerRootCursor = mMusicInfoController.getAllSongsSortByArtist();
		if (mSingerRootCursor == null || mSingerRootCursor.getCount() < 1) {
			Toast.makeText(this, R.string.nosong, Toast.LENGTH_SHORT).show();
			return;
		}
		mSingerRootCursor.moveToFirst();
		mSingerRootAdapter = new SingerRootAdapter();
	}

	private void showSongInfo(String url) {
		Cursor cursor = mMusicInfoController.getSingleSongByURL(url);
		if (cursor != null && cursor.getCount() == 1) {
			cursor.moveToFirst();
			String filename = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));
			String songname = cursor.getString(cursor.getColumnIndex(Media.TITLE));
			String singer = cursor.getString(cursor.getColumnIndex(Media.ARTIST));
			String album = cursor.getString(cursor.getColumnIndex(Media.ALBUM));
			String dataAdd = ConvertNum.formatDateString(MysongActivity.this,
					cursor.getLong(cursor.getColumnIndex(Media.DATE_ADDED)));
			String dataModify = ConvertNum.formatDateString(MysongActivity.this,
					cursor.getLong(cursor.getColumnIndex(Media.DATE_MODIFIED)));
			String duration = ConvertNum.milliToSecs(cursor.getLong(cursor.getColumnIndex(Media.DURATION)));
			String size = ConvertNum.longToSize(cursor.getLong(cursor.getColumnIndex(Media.SIZE)));
			Dialog infoDialog = new AlertDialog.Builder(MysongActivity.this)
					.setIcon(android.R.drawable.btn_star)
					.setTitle(R.string.musicinfo)
					.setMessage(
							"文件名:" + filename + "\r\n歌曲名:" + songname + "\r\n演唱者:" + singer + "\r\n歌曲专辑:" + album + "\r\n添加时间:"
									+ dataAdd + "\r\n修改时间:" + dataModify + "\r\n歌曲时长:" + duration + "\r\n歌曲大小:" + size
									+ "\r\n歌曲路径:" + url).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}).create();
			infoDialog.show();
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		} else {
			Toast.makeText(this, R.string.noinfo, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu_mysong, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// 得到当前选中的MenuItem的ID
		int itemId = item.getItemId();
		switch (itemId) {
		case R.id.fresh:
			if (layoutIndex == LOCALSONG_LAYOUT && local_selected_button == ALLSONG) {
				showAllSongList();
				handler.sendEmptyMessage(ALLSONG);
			} else if (layoutIndex == LOCALSONG_LAYOUT && local_selected_button == FOLDER) {
				showFolder(folderPath);
				handler.sendEmptyMessage(FOLDER);
			} else if (layoutIndex == LOCALSONG_LAYOUT && local_selected_button == SINGER && singerArtist.length() == 0) {
				showSingerRootList();
				handler.sendEmptyMessage(SINGER_ROOT);
			} else if (layoutIndex == LOCALSONG_LAYOUT && local_selected_button == SINGER && singerArtist.length() > 0) {
				showSingerDetailList(singerArtist);
				handler.sendEmptyMessage(SINGER_DETAIL);
			} else if (layoutIndex == RECENT_LAYOUT) {
				showRecentList();
				handler.sendEmptyMessage(RECENT);
			}
			break;
		case R.id.exit:
			Builder builder = new Builder(MysongActivity.this);
			builder.setMessage("确认退出吗？");
			builder.setTitle("提示");
			builder.setPositiveButton("确认", new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					ActivityManger.getInstance().exit();
				}
			});
			builder.setNegativeButton("取消", new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();
			break;
		}
		return true;
	}

}
